import 'package:aitao_flutter/common_ui/confirm_btn.dart';
import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:aitao_flutter/utils/log_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../model/withdraw_model.dart';
import '../../request/api.dart';
import '../../request/requestManager.dart';

class WithDrawPage extends StatefulWidget {
  const WithDrawPage({super.key});

  @override
  State<WithDrawPage> createState() => _WithDrawPageState();
}

class _WithDrawPageState extends State<WithDrawPage> {
  FocusNode userFocusNode = FocusNode();
  WithDrawModel? withDrawModel;
  TextEditingController controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorStyle.themeColor,
        body: GestureDetector(
          onTap: () {
            userFocusNode.unfocus(); // 点击空白收起键盘
          },
          child: ListView(
            children: [withdrawHeader(), withdrawInput(), withdrawAccount()],
          ),
        ));
  }

  Widget withdrawHeader() {
    return Container(
      color: ColorStyle.themeColor,
      padding: EdgeInsetsStyle.customMargin(14, 14, 14, 36),
      child: Column(children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            GestureDetector(
              child: const Icon(
                Icons.arrow_back_ios_new_outlined,
                color: ColorStyle.whiteColor,
              ),
              onTap: () {
                Get.back();
              },
            ),
            GestureDetector(
              child: Text(
                '提现记录',
                style: Styles.whiteText_14,
              ),
              onTap: () {
                Get.toNamed(Routes.withDrawListPage);
              },
            )
          ],
        ),
        Box.vBox(50),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Text(
              withDrawModel?.incomeMoney.toString() ?? '0',
              style: Styles.whiteText_22,
            ),
            Text(
              '=',
              style: Styles.whiteText_22,
            ),
            Text(
              '${withDrawModel?.maxMoney ?? 0}元',
              style: Styles.whiteText_22,
            ),
          ],
        ),
        Box.vBox10,
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Text(
              '可提现花瓣',
              style: Styles.whiteText_16,
            ),
            Text(
              '',
              style: Styles.whiteText_22,
            ),
            Text(
              '可提现人民币',
              style: Styles.whiteText_16,
            ),
          ],
        )
      ]),
    );
  }

  Widget withdrawInput() {
    return Container(
      padding: EdgeInsetsStyle.customMargin(45, 17, 45, 25),
      color: ColorStyle.whiteColor,
      child: Column(children: [
        Text(
          '请输入提现金额',
          style: Styles.normalText_18,
        ),
        Box.vBox25,
        Container(
          alignment: Alignment.center,
          height: 40,
          margin: EdgeInsetsStyle.only(left: 30, right: 30),
          padding: EdgeInsetsStyle.only(left: 50, right: 50),
          decoration: BoxDecoration(
              color: const Color(0xFFFF5B5B),
              borderRadius: RadiusStyle.radiusAll(20)),
          child: TextField(
            controller: controller,
            style: const TextStyle(fontSize: 20, color: Colors.white),
            focusNode: userFocusNode,
            onChanged: (text) {
              controller.text = text;
            },
            decoration: const InputDecoration(
                border: InputBorder.none,
                // labelStyle: TextStyle(fontSize: 20, color: Colors.white),
                hintText: '请输入提现金额',
                hintStyle: TextStyle(fontSize: 14, color: Colors.white)),
            keyboardType: TextInputType.number,
            textAlign: TextAlign.center,
          ),
        ),
        Container(
          padding: EdgeInsetsStyle.only(left: 30, right: 30, top: 10),
          child: Text(
            withDrawModel?.rule ?? '提现规则:申请提现后将在第二天进行审核,审核通过会在下午6点前到帐',
            style: Styles.greyText_11,
          ),
        ),
      ]),
    );
  }

  Widget withdrawAccount() {
    return GestureDetector(
      child: Container(
        color: Colors.white,
        padding: EdgeInsetsStyle.customMargin(21, 0, 21, 200),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text(
            '收款账号',
            style: Styles.normalText_16,
          ),
          Box.vBox15,
          Container(
            padding: EdgeInsetsStyle.edgeInsets(30, 15),
            decoration: BoxDecoration(
                color: ColorStyle.f5f5f5Color,
                borderRadius: RadiusStyle.radiusAll(30)),
            child: Row(children: [
              Image.asset(ImagePath.assetsPayZfb),
              Box.hBox10,
              Text(
                '支付宝',
                style: Styles.normalText_17,
              ),
              Expanded(
                  child: Text(
                withDrawModel?.alipayAccount ?? '请添加支付宝账号',
                style: Styles.greyText_13,
                textAlign: TextAlign.center,
              )),
              const Icon(
                Icons.arrow_forward_ios_rounded,
                color: ColorStyle.color777777,
              )
            ]),
          ),
          Box.vBox40,
          ConfirmButton(
            title: '申请提现',
            backGroundColor: ColorStyle.themeColor,
            titleColor: Colors.white,
            callBack: () {
              if (controller.text.isEmpty) {
                EasyLoading.showToast('请输入提现金额');
                return;
              }
              withDrawClick();
            },
          )
        ]),
      ),
      onTap: () {
        if (withDrawModel == null) return;
        if (withDrawModel!.alipayAccount!.isNotEmpty &&
            withDrawModel?.alipayStatus == 1) {
          EasyLoading.showToast('支付宝账号已绑定');
          return;
        }
        Get.toNamed(Routes.bingAccountPage);
      },
    );
  }

  @override
  void initState() {
    super.initState();
    getWithDrawData();
  }

  void getWithDrawData() async {
    var result = await RequestManager().post(API.walletWithdraw);
    if (result != null && result['code'] == 0) {
      setState(() {
        withDrawModel = WithDrawModel.fromJson(result['data']);
      });
    }
  }

  void withDrawClick() async {
    var result = await RequestManager()
        .post(API.withDrawApply, params: {'money': controller.text});
    WJLogUtils.wjLog(result);
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast(result['message'].toString());
      Get.back();
    }
  }
}
